package org.zxl.iotest.original.blocking;

import java.io.IOException;
import java.net.*;

public class DatagramSocket2 {
    public static void main(String[] args) throws SocketException, UnknownHostException , IOException {
        //创建socket，绑定端口4321
        DatagramSocket zeng = new DatagramSocket(4321);
        System.out.println("zeng ready!!!!");
        byte[] bytes = new byte[1024];
        DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length);
        //接受数据
        zeng.receive(datagramPacket);//阻塞动作，一直等待返回
        //从返回数据报包中取数据
        byte[] data = datagramPacket.getData();
        int length = datagramPacket.getLength();

        String s = new String(data ,0 ,length);
        System.out.println("the zhang 给你发来消息，内容是： \r\n"+s);

        bytes = "I am zeng, nice to meet you,hao are you!".getBytes();
        datagramPacket = new DatagramPacket(bytes, bytes.length, InetAddress.getLocalHost(), 1234);
        zeng.send(datagramPacket);

    }
}
